package com.sbx.controller;

import com.sbx.entity.dto.LoginUser;
import com.sbx.entity.dto.User;
import com.sbx.service.UserService;
import com.sbx.utils.Result;
import com.sbx.validations.NotRequiredField;
import com.sbx.validations.RequiredField;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Resource
    private UserService userService;
    // http://localhost:xx/user/  post
    // http://localhost:xx/user/{id} delete
    // get http://localhost:xx/user/id=xx&username=xx
    // http://localhost:xx/manger
    // http://localhost:xx/xxxx


    /**
     * 新增
     */
    @PostMapping
    @ApiOperation("新增用户")
    public Result insert(@RequestBody @Validated(value = RequiredField.class) User user) {
        userService.insert(user);
        return Result.success();
    }

    @ApiOperation("删除用户")
    @DeleteMapping("{id}")

    public Result deleteById(@PathVariable @ApiParam("用户id") Integer id) {
        userService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询用户")

    public Result getUserPage(@Validated(value = NotRequiredField.class) User user,
                              @ApiParam("起始页") @RequestParam(defaultValue = "1") Integer pageIndex,
                              @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {

        return Result.page(userService.getUserPage(user, pageIndex, pageSize));
    }

    @PutMapping
    @ApiOperation("用户信息修改")

    public Result update(@RequestBody @Validated(NotRequiredField.class) User user) {
        userService.update(user);
        return Result.success();
    }


    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody @Validated(value = RequiredField.class) User user) {

        userService.registerByEmail(user);

        return Result.success();
    }

    @GetMapping("/{email}/confirmEmail/{code}")
    @ApiOperation("校验邮箱")
    public Result confirmEmail(@PathVariable @ApiParam("用户邮箱") String email, @PathVariable @ApiParam("验证码") String code) {
        String info = userService.confirmEmail(email, code);
        return Result.success(info);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result userLogin(@RequestBody LoginUser loginUser, HttpServletResponse response) {

        User user = userService.userLogin(loginUser, response);

        return Result.success(user);
    }

    @GetMapping("{token}/tokenByUserInfo")
    @ApiOperation("根据token获取用户信息")
    public Result tokenByUserInfo(@PathVariable @ApiParam("用户token") String token) {
        User user = userService.tokenByUserInfo(token);
        return Result.success(user);
    }

    @GetMapping("{token}/logout")
    @ApiOperation("用户退出登录")
    public Result userLogout(@PathVariable @ApiParam("用户token") String token) {
        userService.userLogout(token);
        return Result.success();
    }


}
